import _thread
import time
import sys
import network
from machine import Pin
import run
import ota 

led1=Pin(12, Pin.OUT, value=0)
led2=Pin(13, Pin.OUT, value=0)

runObj = run.Run()

# 连接wifi
def ConnectWlan(ssid, password):
    sta_if = network.WLAN(network.STA_IF)
    ap_if = network.WLAN(network.AP_IF)
    sta_if.active(True)
    ap_if.active(False)

    if not sta_if.isconnected():
        print("Connecting to WLAN ({})...".format(ssid))
        sta_if.active(True)
        sta_if.connect(ssid, password)
        while not sta_if.isconnected():
            pass
    print("network config:",sta_if.ifconfig())
    return True 
 
def main(*args, **kwargs):
    # Wi-Fi credentials
    time.sleep(2)
    SSID = "501"
    PASSWORD = "13048271212a"
    print("wifi connect")
    ConnectWlan(SSID, PASSWORD)
    runObj.run()
    print("wifi ok")
    
 
def otaInit(*args, **kwargs):
    time.sleep(5)
    while True:
        print("进行检测ota")
        print("OtaUpdate",ota.OtaUpdate)
        ota.OtaUpdate()
        time.sleep(5)
 
# 此处创建2个线程
thread_1 = _thread.start_new_thread(main, (1,))
thread_2 = _thread.start_new_thread(otaInit, (2,))
